package net.ermannofranco.xml.schema;

import net.ermannofranco.xml.Static;

public class RestrictionComplexContent extends SchemaTag {

	private static final long serialVersionUID = 1030198410185911121L;
	private Content gestoreContenuto;

	protected RestrictionComplexContent(int level, SchemaTag container) {
		super("restriction", level, container);
	}

	/**
	 * @param simpleTypeNumber
	 * @return
	 */
	private RestrictionComplexContent setBase(String typeName) {
		addAttr("base", feedFormPrefix(typeName));
		return this;
	}

	/**
	 * @param simpleTypeNumber
	 * @return
	 */
	protected RestrictionComplexContent setBase(ContentType type) {
		setBase(type.getTypeName());
		return this;
	}

	protected RestrictionComplexContent setBase(XmlType type) {
		addAttr("base", getLocalNS() + Static.COLON + XmlType.getType(type));
		return this;
	}

	// public Attribute addAttributeTag() {
	// // if (gestoreContenuto == 0)
	// // throw new SchemaException(
	// // "Assegnare contenuto prima di addAttributeTag");
	// Attribute att;
	// add(att = new Attribute(getLevel() + 1, this));
	// return att;
	// }

	/**
	 * add a "Attribute" tag with given name.
	 * 
	 * @return Attribute
	 */
	public Attribute addTagAttribute(String name) {
		// if (gestoreContenuto == 0)
		// throw new SchemaException(
		// "Assegnare contenuto prima di addAttributeTag");
		Attribute att;
		add(att = new Attribute(getLevel() + 1, this));
		att.setName(name);
		return att;
	}

	/**
	 * add a "Attribute" tag with given name and use.
	 * 
	 * @return Attribute
	 */
	public Attribute addTagAttribute(String name, Use use) {
		Attribute ret = addTagAttribute(name);
		ret.setUse(use);
		return ret;
	}

	/**
	 * add a "Attribute" tag with given reference.
	 * 
	 * @return this
	 */
	public RestrictionComplexContent addTagAttribute(Attribute ref) {
		// if (gestoreContenuto == 0)
		// throw new SchemaException(
		// "Assegnare contenuto prima di addAttributeTag");
		Attribute att;
		add(att = new Attribute(getLevel() + 1, this));
		att.setRef(ref);
		return this;
	}

	/**
	 * add a "Attribute" tag with given reference and use.
	 * 
	 * @return this
	 */
	public RestrictionComplexContent addTagAttribute(Attribute ref, Use use) {
		Attribute att;
		add(att = new Attribute(getLevel() + 1, this));
		att.setRef(ref);
		att.setUse(use);
		return this;
	}

	/**
	 * add a "Attribute" tag with given name and type.
	 * 
	 * @return this
	 */
	public RestrictionComplexContent addTagAttribute(String name, XmlType type) {
		Attribute ret = addTagAttribute(name);
		ret.setType(type);
		return this;
	}

	/**
	 * add a "Attribute" tag with given name, type and use.
	 * 
	 * @return this
	 */
	public RestrictionComplexContent addTagAttribute(String name, XmlType type,
			Use use) {
		Attribute ret = addTagAttribute(name);
		ret.setType(type).setUse(use);
		return this;
	}

	/**
	 * add a "Attribute" tag with given name and type reference.
	 * 
	 * @return Attribute
	 */
	public Attribute addTagAttribute(String name, ContentType typeRef) {
		Attribute ret = addTagAttribute(name);
		ret.setType(typeRef);
		return ret;
	}

	/**
	 * add a "Attribute" tag with given name, type reference and use.
	 * 
	 * @return Attribute
	 */
	public Attribute addTagAttribute(String name, ContentType typeRef, Use use) {
		Attribute ret = addTagAttribute(name, typeRef);
		ret.setUse(use);
		return ret;
	}

	/**
	 * add a "Attribute" tag with given name and type name.
	 * 
	 * @return Attribute
	 */
	public Attribute addTagAttribute(String name, String typeName) {
		Attribute ret = addTagAttribute(name);
		ret.setType(typeName);
		return ret;
	}

	/**
	 * add a "Attribute" tag with given name, type name and use.
	 * 
	 * @return Attribute
	 */
	public Attribute addTagAttribute(String name, String typeName, Use use) {
		Attribute ret = addTagAttribute(name, typeName);
		ret.setUse(use);
		return ret;
	}

	/**
	 * Add an already declared first level AttributeGroup reference.
	 * 
	 * @return
	 */
	public AttributeGroup addAttributeGroup(AttributeGroup ref) {
		// if (gestoreContenuto == 0)
		// throw new SchemaException(
		// "Assegnare contenuto prima di addAttributeGroup");
		AttributeGroup att;
		add(att = new AttributeGroup(getLevel() + 1, this));
		att.setRef(ref);
		return att;
	}

	/**
	 * @return
	 */
	public Sequence setSequence() {
		if (gestoreContenuto != null)
			throw new SchemaException("Contenuto di ComplexType gia' assegnato");
		Sequence s = new Sequence(getLevel() + 1, this, true);
		add(s);
		gestoreContenuto = Content.SEQUENCE;
		return s;
	}

	/**
	 * @return
	 */
	public Choice setChoice() {
		if (gestoreContenuto != null)
			throw new SchemaException("Contenuto di ComplexType gia' assegnato");
		Choice s = new Choice(getLevel() + 1, this);
		add(s);
		gestoreContenuto = Content.CHOICE;
		return s;
	}

	/**
	 * @return
	 */
	public Group setGroup() {
		if (gestoreContenuto != null)
			throw new SchemaException("Contenuto di ComplexType gia' assegnato");
		Group s = new Group(getLevel() + 1, this);
		add(s);
		gestoreContenuto = Content.GROUP;
		return s;
	}

}
